package com.peas.platform.module.icims.base.sys.component;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.peas.platform.common.core.configuration.RequestContextUtil;
import com.peas.platform.common.core.constant.Constants;
import com.peas.platform.common.core.property.ProjectProperties;
import com.peas.platform.framework.mybatisplus.interceptor.BaseInnerInterceptor;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.dto.RecycleDto;
import com.peas.platform.module.icims.base.sys.service.RecycleService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@Slf4j
public class CustomDeleteInterceptor implements BaseInnerInterceptor {

    @Resource
    private ApplicationContext ac;
    @Resource
    private RecycleService recycleService;
    @Resource
    private ProjectProperties projectProperties;

    @SneakyThrows
    private void recycle(String method, String sql, Map<String, Object> param) {
        String c = method.substring(0, method.lastIndexOf('.'));
        String m = method.substring(method.lastIndexOf('.') + 1);
        log.warn("c:" + c);
        log.warn("m:" + m);
        String seq = Long.toHexString(Constants.snowflake.nextId());
        BaseRepository<?> repo = (BaseRepository<?>) ac.getBean(Class.forName(c));
        switch (m) {
            case "deleteById":
                BasePo po = repo.selectById((Long) param.get("id"));
                if (po != null) {
                    String content = JSONUtil.toJsonStr(po);
                    JSONObject json = JSON.parseObject(content);
                    String table = sql.split(" ")[2];
                    table = table.substring(1, table.length() - 1);
                    RecycleDto dto = new RecycleDto();
                    dto.setSystem(projectProperties.getBusiness());
                    dto.setSystemName(projectProperties.getBusinessName());
                    dto.setService(projectProperties.getService());
                    dto.setServiceName(projectProperties.getServiceName());
                    dto.setTableName(table);
                    dto.setTableId(json.getLong("id"));
                    dto.setContent(content);
                    dto.setDeleteSeq(seq);
                    dto.setDeleteTime(DateTime.now());
                    dto.setDeleteBy(Long.valueOf(RequestContextUtil.getCurrentUserId(true)));
                    dto.setDeleteRemark(null);
                    recycleService.save(dto);
                }
                break;
            case "delete":
                break;
        }
    }

    @Override
    public Integer sort() {
        return 10;
    }

    @Override
    public void beforeSelect(String method, String sql, Map<String, Object> param) {
    }

    @Override
    public void beforeInsert(String method, String sql, Map<String, Object> param) {
    }

    @Override
    public void beforeUpdate(String method, String sql, Map<String, Object> param) {
    }

    @Override
    public void beforeDelete(String method, String sql, Map<String, Object> param) {
        recycle(method, sql, param);
    }

}
